package com.touka.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.touka.common.Result;
import com.touka.entity.Category;
import com.touka.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
  @Autowired
  private CategoryService categoryService;

  private static final LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery(Category.class);

  /**
   * 分页查询分类列表
   *
   * @param page
   * @param pageSize
   * @return
   */
  @GetMapping("/page")
  public Result<Page<Category>> list(@DefaultValue("1") Long page, @DefaultValue("10") Long pageSize) {
    log.info("分页查询分类：{},{}", page, pageSize);

    Page<Category> pageCate = new Page<>(page, pageSize);

    Page<Category> categoryPage = categoryService.page(
            pageCate,
            queryWrapper.orderByAsc(Category::getSort)
    );
    return Result.success(categoryPage);
  }

  /**
   * 新增菜品or套餐分类
   *
   * @param category
   * @return
   */
  @PostMapping
  public Result<String> insert(@RequestBody Category category) {
    log.info((category.getType() == 1 ? "新增菜品" : "新增套餐") + "分类：{}", category);

    Category category1 = categoryService.getByName(category.getName());
    if (category1 == null) {
      boolean saveOrUpdate = categoryService.save(category);
      return saveOrUpdate ? Result.success("操作成功") : Result.error("操作失败");
    } else if (category1.getIsDeleted() == 1) {

      categoryService.updateIsDeleted(category1.getId(), category.getSort());
      return Result.success("操作成功");
    }
    return Result.error("该分类已存在");
  }

  /**
   * 根据Id获取分类信息
   *
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  public Result<Category> getById(Long id) {
    Category category = categoryService.getById(id);
    return Result.success(category);
  }

  /**
   * 修改分类
   *
   * @param category
   * @return
   */
  @PutMapping
  public Result<String> update(@RequestBody Category category) {
    log.info("修改分类：{}", category);
    boolean update = categoryService.updateById(category);
    return update ? Result.success("修改成功") : Result.error("修改失败");
  }

  /**
   * 逻辑删除
   *
   * @param ids
   * @return
   */
  @DeleteMapping
  public Result<String> delete(Long ids) {
    log.info("逻辑删除：{}", ids);

    categoryService.delete(ids);
    return Result.success("删除成功");
  }

  /**
   * 根据type查询分类
   *
   * @param type
   * @return
   */
  @GetMapping("/list")
  public Result<List<Category>> listByType(Integer type) {
    List<Category> categoryList = categoryService.list(
            queryWrapper.eq(type != null, Category::getType, type)
    );
    return Result.success(categoryList);
  }
}
